'''
Created on Feb 19, 2011

@author: solagna
'''
import ParseCommon
import WikiDownloader.FileManager

__EVENTS_TABLE = 1 #The table where the events are listed.
__PARTECIPATIONS_TABLE = 3
__PUBLICATIONS_TABLE = 4
__ISSUES_TABLE = 5

fm = WikiDownloader.FileManager.FileManager()

def parseTaskEventHeader (partner="SA12"):
    ''' 
    Header should be equal for every page, but it parses a specific wiki page'''
    fm = WikiDownloader.FileManager.FileManager()
    task_file = fm.getTaskFile(partner)
    soup = ParseCommon.soupify(task_file)
    
    return ParseCommon.parseTableYheader(soup, __EVENTS_TABLE)

def parseTaskEventTable (partner, add_name = False):
    '''
    Parse the event table in the report wiki page of the input partner.
    Gets the filename using the FileManager modules.
    Returns a list.
    If add_name, pre-pend the partenr name as first field of every row.
    '''
    
    #Get the soup.
    task_file = fm.getTaskFile(partner)
    soup = ParseCommon.soupify(task_file)
    #Parse the file 
    table_list = ParseCommon.parseTableX(soup, __EVENTS_TABLE)
    
    if len(table_list) > 0:
        #Prepend name, if needed.
        if add_name:
            [a.insert(0,partner) for a in table_list]
        return table_list
    else:
        return None
 
def parseallTaskEvents():
    '''    
    Returns a table with all the events organized by all partenrs in the quarter qr.
    '''   
    partner_list = fm.getTaskPartners()
    table_total = list()
    for part in partner_list:
        table_part = parseTaskEventTable(part, True) #Prepend the partenr name
        if table_part is not None:
            table_total.extend(table_part)

    return table_total


if __name__ == "__main__":
    # Test code.
    #print ",".join(fm.getTaskPartners())
    headerlist = parseallTaskEvents()
    for header in headerlist:
        print ' , '.join(header)
    